package com.lecture.av.cameraxachiveh264;

import android.Manifest;
import android.content.pm.PackageManager;
import android.graphics.ImageFormat;
import android.graphics.Rect;
import android.media.Image;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.TextureView;
import android.view.View;


import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.camera.core.ImageProxy;

import com.lecture.av.cameraxachiveh264.CameraX.VideoChanel;
import com.lecture.av.cameraxachiveh264.audio.AudioChannel;
import com.pedro.rtmp.utils.ConnectCheckerRtmp;
import com.pedro.rtplibrary.rtmp.RtmpUsbCamera;

import java.nio.ByteBuffer;


public class MainActivity extends AppCompatActivity {

    private static final String TAG = MainActivity.class.getSimpleName();
    private TextureView textureView;
    private RtmpClient rtmpClient;

    private RtmpUsbCamera usbCamera;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        checkPermission();
        textureView = findViewById(R.id.textureView);

        rtmpClient = new RtmpClient(this);
        //初始化摄像头， 同时 创建编码器
        rtmpClient.initVideo(textureView, 3840, 2160, 10, 640_000);
        rtmpClient.initAudio(44100, 2);

        //初始化推流库
        usbCamera = new RtmpUsbCamera(this, new ConnectCheckerRtmp() {
            @Override
            public void onConnectionStartedRtmp(@NonNull String s) {
                Log.e(TAG, "RtmpUsbCamera:onConnectionStartedRtmp--------------");
            }

            @Override
            public void onConnectionSuccessRtmp() {
                Log.e(TAG, "RtmpUsbCamera:onConnectionSuccessRtmp--------------");
            }

            @Override
            public void onConnectionFailedRtmp(@NonNull String s) {
                Log.e(TAG, "RtmpUsbCamera:onConnectionFailedRtmp--------------");
            }

            @Override
            public void onNewBitrateRtmp(long l) {

            }

            @Override
            public void onDisconnectRtmp() {
                Log.e(TAG, "RtmpUsbCamera:onDisconnectRtmp--------------");
            }

            @Override
            public void onAuthErrorRtmp() {
                Log.e(TAG, "RtmpUsbCamera:onAuthErrorRtmp--------------");
            }

            @Override
            public void onAuthSuccessRtmp() {
                Log.e(TAG, "RtmpUsbCamera:onAuthSuccessRtmp--------------");
            }
        });

        //给推流库喂数据
        rtmpClient.setCamerax(new VideoChanel.camerax() {
            @Override
            public void onAnalyze(ImageProxy image, int rotationDegrees) {
                byte[] bytes = ImageUtil.yuv420ToNv21(image);
                if (usbCamera.isStreaming())
                    usbCamera.setInputYUVData(bytes, rotationDegrees, ImageFormat.YUV_420_888);
            }
        });
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        rtmpClient.release();
    }

    public boolean checkPermission() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && checkSelfPermission(
                Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
            requestPermissions(new String[]{
                    Manifest.permission.READ_EXTERNAL_STORAGE,
                    Manifest.permission.WRITE_EXTERNAL_STORAGE,
                    Manifest.permission.CAMERA,
                    Manifest.permission.RECORD_AUDIO
            }, 1);

        }
        return false;
    }

    public void toggleCamera(View view) {
        rtmpClient.toggleCamera();
    }


    public void startLive(View view) {
//        rtmpClient.startLive("rtmp://47.106.114.236:11006/live/livestream");
        if (usbCamera.prepareAudio() && usbCamera.prepareVideo(3840, 2160, 1200 * 1024)) {
            Log.e(TAG, "startLive:开始推流--------------");
            usbCamera.startStream("rtmp://47.106.114.236:11006/live/livestream");
        }
    }

    public void stopLive(View view) {
//        rtmpClient.stopLive();
        Log.e(TAG, "startLive:关闭推流--------------");
        usbCamera.stopStream();
    }


}